package cn.com.controller.administrator;

import cn.com.dao.PerDao;
import cn.com.entity.Clazz;
import cn.com.entity.Student;
import cn.com.entity.Teacher;
import cn.com.tool.Jdbc;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Project:Academic - PerController
 *
 * @author 黄俊玮 On Administrator
 * @version 1.0
 * @DataTime 2024/6/14 08:50:44
 * @since 1.8
 */
public class PerController {
    private PerDao perDao = new PerDao();

    public void doStudent(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException{
        List<Student> list =  perDao.doStudent();
        request.setAttribute("doStudent",list);
        request.getRequestDispatcher("/WEB-INF/jsp/manager/personalInfo/student/show.jsp").forward(request,response);
    }
    public void goStu(HttpServletRequest request,HttpServletResponse response) throws  IOException , ServletException{
        int cid = Integer.parseInt(request.getParameter("cid"));
        String sname = request.getParameter("sname");
        int user = Integer.parseInt(request.getParameter("phone"));
        String pwd = request.getParameter("pwd");
        String clazz = request.getParameter("clazz");
        String cname = request.getParameter("cname");

        List<Clazz> list = perDao.goStu(cid,sname,user,pwd,clazz,cname);
        System.out.println(list);

        request.setAttribute("list",list);
        request.getSession().setAttribute("goStu",new Student(cid,sname,user,pwd,clazz,cname));
        request.getRequestDispatcher("/WEB-INF/jsp/manager/personalInfo/student/update.jsp").forward(request,response);
    }
    public void updateStu(HttpServletRequest request,HttpServletResponse response) throws  IOException , ServletException{
        int sid = Integer.parseInt(request.getParameter("id"));
        String sname = request.getParameter("name");
        int phone = Integer.parseInt(request.getParameter("phone"));
        String pwd = request.getParameter("pwd");
        String cid = request.getParameter("cid");//注意页面提交的内容为班级的id
        Student str = (Student) request.getSession().getAttribute("goStu");//原有的用户信息

        boolean key = perDao.updateStu(sid,sname,phone,pwd,cid,str);
        request.getSession().removeAttribute("goStu");
        if (key){
            request.getRequestDispatcher("/Administrator/personStu.do").forward(request,response);
            return;
        }
        request.setAttribute("err","ID或手机号与系统中的重复！！！");
        request.getRequestDispatcher("/Administrator/personStu.do").forward(request,response);
    }
    public void deleteStu(HttpServletRequest request,HttpServletResponse response) throws IOException , ServletException{
        int sid = Integer.parseInt(request.getParameter("cid"));
        int phone = Integer.parseInt(request.getParameter("phone"));
        perDao.delete(sid,phone);
        request.getRequestDispatcher("/Administrator/personStu.do").forward(request,response);
    }
    public void addStu(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException{
        List<Clazz> list = perDao.addStu();
        request.setAttribute("addStu",list);//获取班级信息.
        System.out.println(list);
        request.getRequestDispatcher("/WEB-INF/jsp/manager/personalInfo/student/add.jsp").forward(request,response);

    }
    public void addDoingStu(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException{
        String name = request.getParameter("name");
        long user = Long.parseLong(request.getParameter("phone"));
        String pwd = request.getParameter("pwd");
        String clazz = request.getParameter("cid");
        boolean key = perDao.addDoingStu(name,user,pwd,clazz);
        if (key){
            request.getRequestDispatcher("/Administrator/personStu.do").forward(request,response);
        }else {
            request.setAttribute("err","<span style=\"color:red\">：用户手机号已存在系统中</span>");
            request.getRequestDispatcher("/Administrator/personStu.do").forward(request,response);
        }




    }
    public void doTeacher(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException{
        List<Teacher> list =  perDao.doTeacher();
        request.setAttribute("doTeacher",list);
        request.getRequestDispatcher("/WEB-INF/jsp/manager/personalInfo/teacher/show.jsp").forward(request,response);
    }
    public void upIndex(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException{
        int id = Integer.parseInt(request.getParameter("cid"));
        String name = request.getParameter("sname");
        int user = Integer.parseInt(request.getParameter("user"));
        String pwd = request.getParameter("pwd");
        request.getSession().setAttribute("tea_up",new Teacher(id,name,user,pwd));
        request.getRequestDispatcher("/WEB-INF/jsp/manager/personalInfo/teacher/update.jsp").forward(request,response);
    }
    public void upTeach(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException{
        Teacher t = (Teacher) request.getSession().getAttribute("tea_up");
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        int phone = Integer.parseInt(request.getParameter("user"));
        String pwd = request.getParameter("pwd");
        boolean key = perDao.updateTea(id,name,phone,pwd,t);
        if (key){
            request.getRequestDispatcher("/Administrator/personTea.do").forward(request,response);
            return;
        }
        request.setAttribute("err","ID或手机号与系统中的重复!!!");
        request.getRequestDispatcher("/Administrator/personTea.do").forward(request,response);
    }
    public void addTeacher(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException{
        if (request.getParameter("name") != null){
            String name = request.getParameter("name");
            int phone = Integer.parseInt(request.getParameter("phone"));
            String pwd = request.getParameter("pwd");
            boolean key = perDao.addTeacher(name,phone,pwd);
            if (key){
                request.getRequestDispatcher("/Administrator/personTea.do").forward(request,response);
                return;
            }
            request.setAttribute("err","添加失败");
            request.getRequestDispatcher("/Administrator/personTea.do").forward(request,response);
            return;
        }
        request.getRequestDispatcher("/WEB-INF/jsp/manager/personalInfo/teacher/add.jsp").forward(request,response);
    }
}
